﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcMusicStore.Models;
using MvcMusicStore.Helpers;

namespace MvcMusicStore.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class StoreManagerController : Controller
    {
        MusicStoreEntities storeDB = new MusicStoreEntities();
        //
        // GET: /StoreManager/

        public ActionResult Index()
        {
            var albums = storeDB.Albums.Include("Genre").Include("Artist").ToList();
            return View(albums);
        }

        
        //
        // GET: /StoreManager/Create

        public ActionResult Create()
        {
            ViewBag.Geners = storeDB.Genres.OrderBy(p => p.Name).ToList();
            ViewBag.Aritist = storeDB.Artists.OrderBy(p => p.Name).ToList();
            var album = new Album();
            return View(album);
        }

        //
        // POST: /StoreManager/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                if (ModelState.IsValid == true)
                {
                    Album album = new Album();
                    album.ArtistId = Convert.ToInt32(collection["ArtistId"]);
                    album.GenreId = Convert.ToInt32(collection["GenreId"]);
                    album.Title = collection["Title"];
                    album.Price = Convert.ToDecimal(collection["Price"]);

                    storeDB.Albums.Add(album);
                    storeDB.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    return RedirectToAction("Create");
                }
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /StoreManager/Edit/5

        public ActionResult Edit(int id)
        {
            ViewBag.Geners = storeDB.Genres.OrderBy(p=>p.Name).ToList();
            ViewBag.Aritist = storeDB.Artists.OrderBy(p=>p.Name).ToList();
            var album= storeDB.Albums.Find(id);
            return View(album);
        }

        //
        // POST: /StoreManager/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                var album = storeDB.Albums.Find(id);
                bool  isSuccessful= TryUpdateModel(album);
                if (isSuccessful == true)
                {
                    storeDB.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    ViewBag.Geners = storeDB.Genres.OrderBy(p => p.Name).ToList();
                    ViewBag.Aritist = storeDB.Artists.OrderBy(p => p.Name).ToList();
                    return View(album); 
                }
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /StoreManager/Delete/5

        public ActionResult Delete(int id)
        {
            var album = storeDB.Albums.Find(id);
            return View(album);
        }

        //
        // POST: /StoreManager/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var album= storeDB.Albums.Find(id);
                storeDB.Albums.Remove(album);
                storeDB.SaveChanges();

                ViewBag.ControllerName = HtmlHelpers.GetControllerName<StoreManagerController>();
                return View("Deleted");
            }
            catch
            {
                return View();
            }
        }
    }
}
